<template>
    <div>
        <!-- 导航部分开始 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>排班管理</el-breadcrumb-item>
            <el-breadcrumb-item>排班计划</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 导航部分结束 -->

        <!--  搜索栏开始 -->
        <el-form style="margin-top: 15px;" :inline="true" :model="queryTeam" class="demo-form-inline">
            <el-form-item label="班组编号">
                <el-input v-model="queryTeam.teamCode" placeholder="请选择班组类型" clearable />
            </el-form-item>
            <el-form-item label="班组类型">
                <el-input v-model="queryTeam.calendarType" placeholder="请输入班组编号" clearable />
            </el-form-item>
            <el-form-item label="班组名称">
                <el-input v-model="queryTeam.teamName" placeholder="请输入班组名称" clearable />
            </el-form-item>
     

            <el-form-item>
                <el-button type="primary" @click="loadTeams">搜索</el-button>
            </el-form-item>
        </el-form>
        <!--  搜索栏结束 -->

        <!-- 工具栏开始 -->
        <div class="flex">
            <el-button title="新增班组" @click="this.addTeamDialogVisible = true" type="primary"><el-icon>
                    <Plus />
                </el-icon></el-button>
        </div>
        <!-- 工具栏结束 -->
         <!-- 表格 开始 -->
         <el-table ref="teamTable" :data="teams" max-height="800px" stripe="true" border="true"
            style="width: 90%;margin-top: 10px;">
            <el-table-column align="center" type="selection" width="55" />
            <el-table-column align="center" label="#" property="teamId" />
            <el-table-column align="center" label="班组编号" property="teamCode" />
            <el-table-column align="center" label="班组名称" property="teamName" />
            <el-table-column align="center" label="班组类型" property="calendarType"/>
            <el-table-column align="center" label="备注" property="remark"/>
            <el-table-column width="150" align="center" label="操作">
                <template #default="scope">
                    <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-popconfirm confirm-button-text="是" cancel-button-text="点错了" @confirm="handleDelete(scope.row)"
                        title="您是否要删除这行数据">
                        <template #reference>
                            <el-button size="small" type="danger">删除</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!-- 表格 结束 -->

        <!-- 分页栏开始 -->
        <el-pagination style="margin-top: 15px;" :page-size="teamLimit" :pager-count="teamMaxPage"
            @current-change="changePage" background layout="prev, pager, next" :total="teamTotal" />
        <!-- 分页栏结束 -->

       <!-- 添加班组的弹窗窗体开始-->
       <el-dialog v-model="addTeamDialogVisible" title="添加班组" width="600" align-center>
            <!-- 添加用户的表单 -->
            <el-form label-width="100px" :model="newTeam" style="max-width: 460px">
                <el-form-item label="班组编号">
                    <el-input v-model="newTeam.teamCode" />
                </el-form-item>
                <el-form-item label="班组名称">
                    <el-input v-model="newTeam.teamName" />
                </el-form-item>
                <el-form-item label="班组类型">
                    <el-input v-model="newTeam.calendarType" />
                </el-form-item>
                <el-form-item label="备注">
                    <el-input type="textarea" :rows="2" v-model="newTeam.remark" />
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="cancelAddTeam">取消</el-button>
                    <el-button type="primary" @click="doAddTeam">
                        提交
                    </el-button>
                </span>
            </template>
        </el-dialog>
        <!-- 添加用户的弹窗窗体结束-->


        <!-- 编辑班组的弹窗开始 -->
        <el-dialog v-model="teamEditDrawer" width="36%" align-center>
            <template #header>
                <h4>修改班组类型</h4>
            </template>
            <template #default>
                <div>
                    <!-- 编辑班组的表单 -->
                    <el-form label-width="100px" :model="editTeam" style="max-width: 460px">
                        <el-form-item label="班组名称">
                            <el-input v-model="editTeam.teamName" />
                        </el-form-item>
                        <el-row justify="space-between">
                            <el-col :span="10">
                                <el-form-item label="班组编号">
                                    <el-select v-model="editTeam.teamCode" class="m-2" placeholder="Select" size="large">
                                        <el-option v-for="item in indexTypeDate" :key="item.value" :label="item.label"
                                            :value="item.value" />
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="班组类型">
                                    <el-select v-model="editTeam.calendarType" class="m-2" placeholder="Select"
                                        size="large">
                                        <el-option v-for="item in teamLevelDate" :key="item.value" :label="item.label"
                                            :value="item.value" />
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </div>
            </template>
            <template #footer>
                <div style="flex: auto">
                    <el-button @click="cancelEdit">取消</el-button>
                    <el-button type="primary" @click="doTeamEdit">确定</el-button>
                </div>
            </template>
        </el-dialog>
        <!-- 编辑班组的弹窗结束 -->
    </div>
</template>

<script>
import teamRequest from '../../../api/team-api';
export default {
    data() {
        return {
            queryTeam: {},// 分装查询班组条件的班组对象
            teams: [],//班组数据列表
            teamCurrPage: 1, // 班组分页的当前页码
            teamLimit: 10, // 班组分页的每页大小
            teamTotal: 0, // 班组列表的总条数
            teamMaxPage: 1,// 班组列表的最大页码
            // 新增班组相关属性
            addTeamDialogVisible: false,// 新增班组的弹窗标志
            newTeam: {},// 封装新增的班组信息
            currTeam: {},// 保存正在操作的班组信息

            //编辑班组相关属性
            teamEditDrawer: false,//修改班组的弹窗标志
            editTeam: {},//封装修改的班组信息
        }
    },
    methods: {
        /**
         * 班组列表的翻页函数
         * @param {} value  最新页码
         */
         changePage(value) {
            this.teamCurrPage = value;
            this.loadTeams();
        },
        /**
         * 行删除事件
         * @param {} row 
         */
        handleDelete(id) {
            console.log(id);
            teamRequest.delete(id,res=>{
                if(res.code == 200){
                    this.$message({
                        type:'success',
                        message:'删除成功'
                    });
                    this.loadTeams();
                }else{
                    this.$message({
                        type:'error',
                        message:'删除失败'
                    })
                }
            })
        },
         /**
         * 确定编辑班组
         */
         doTeamEdit() {
            console.log("开始编辑")
            // 提交editUser即可
            teamRequest.edit(this.editTeam, res => {
                console.log("*******")
                // 这里的res就是我们的后端返回的perResult对象
                if (res.code == '200') {
                    this.$message({
                        message: '修改字典成功',
                        type: 'success'
                    });
                    //关闭窗体
                    this.teamEditDrawer = false;
                    // 重新加载数据
                    this.loadTeams();
                    // 将editUser制空
                    this.editTeam = {};
                } else if (res.code == '300') {
                    this.$message({
                        message: '字典重复',
                        type: 'warning',
                    });
                } else {
                    this.$message({
                        message: '编辑失败',
                        type: 'error',
                    });
                }
            });
        },
        /**
         * 取消班组编辑
         */
         cancelEdit() {
            // 将编辑的用户制空
            this.editTeam = {};
            // 关闭抽屉弹窗
            this.teamEditDrawer = false;
        },
        /**
         * 行编辑事件
         * @param {*} row 
         */
        handleEdit(row) {
            teamRequest.selectByTeamId(row.teamId, res => {
                console.log(row.teamId);
                this.teamEditDrawer = true;
                this.editTeam = res.data;
            })

        },
        /**
         * 新增班组的提交按钮事件
         */
        doAddTeam() {
            // 数据校验：略
            // 提交数据
            teamRequest.save(this.newTeam, res => {
                if (res.code == 200) {
                    this.$message({
                        type: "success",
                        message: "保存成功"
                    });
                    // 关闭窗体
                    this.cancelAddTeam();
                    // 重新加载数据
                    this.loadTeams();// 加载当前页
                } else if (res.code == 300) {
                    this.$message({
                        type: "warning",
                        message: res.msg
                    });
                } else {
                    this.$message({
                        type: "error",
                        message: "保存失败"
                    });
                }
            });
        },
        /**
         * 取消新增班组的按钮事件
         */
        cancelAddTeam() {
            // 关闭窗体
            this.addTeamDialogVisible = false;
            // 清空newDict
            this.newTeam = {};
        },
        resetting() {
            teamRequest.list(this.teamCurrPage, this.teamLimit, this.queryTeam = {}, res => {
                // 设置数据
                this.teams = res.data.list;
                this.teamTotal = res.data.total;
                this.teamMaxPage = res.data.pages;
            });
        },
        loadTeams() {
            teamRequest.list(this.TeamCurrPage, this.TeamLimit, this.queryTeam, res => {
                // 设置数据
                this.teams = res.data.list;
                console.log(res.data.list);
                this.TeamTotal = res.data.total;
                this.TeamMaxPage = res.data.pages;
            });
        }
    },
    mounted() {
        // 默认加载字典数据列表
        this.loadTeams();
    }
}
</script>